
package com.hlkj.pay.controller.ad;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.ad.IAdMediaAppService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.ad.query.AdMediaQueryDto;
import com.hlkj.pay.infrastructure.model.ad.AdMediaDO;
import com.hlkj.pay.service.ad.IAdMediaService;
import com.hlkj.pay.vo.ad.req.AdMediaPageQueryReq;
import com.hlkj.pay.vo.ad.req.AdMediaReq;
import com.hlkj.pay.vo.ad.resp.AdMediaResp;
import com.hlkj.pay.vo.common.req.DeleteIdReq;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.utils.BeanUtil;

/**
 * @author HlpayTeam
 * @date 2024/10/22 18:23
 */
@Tag(name = "广告媒介管理")
@RestController
@RequestMapping("/v1/ad/media")
@RequiredArgsConstructor
public class AdMediaController extends BaseController {

    private final IAdMediaAppService adMediaAppService;

    private final IAdMediaService adMediaService;

    @Operation(summary = "分页查询")
    @GetMapping("/page")
    CommonResult<PageResult<AdMediaResp>> queryAdMediaPage(@Validated AdMediaPageQueryReq pageParam) {
        AdMediaQueryDto adMediaQueryDto = new AdMediaQueryDto();
        adMediaQueryDto.setLikeName(pageParam.getName());
        adMediaQueryDto.setPageNo(pageParam.getPageNo());
        adMediaQueryDto.setPageSize(pageParam.getPageSize());
        PageResult<AdMediaDO> page = adMediaService.queryPage(adMediaQueryDto);
        List<AdMediaDO> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        return buildPageResult(page, cn.hutool.core.bean.BeanUtil.copyToList(list, AdMediaResp.class));
    }

    @Operation(summary = "不分页查询")
    @GetMapping("/list")
    CommonResult<List<AdMediaResp>> queryAdMediaList(@Validated AdMediaPageQueryReq pageParam) {
        AdMediaQueryDto adMediaQueryDto = new AdMediaQueryDto();
        adMediaQueryDto.setLikeName(pageParam.getName());
        List<AdMediaDO> list = adMediaService.queryList(adMediaQueryDto);
        if (CollectionUtils.isEmpty(list)) {
            CommonResult.success(Collections.emptyList());
        }
        return CommonResult.success(cn.hutool.core.bean.BeanUtil.copyToList(list, AdMediaResp.class));
    }

    @Operation(summary = "详情")
    @GetMapping("/detail")
    CommonResult<AdMediaResp> detail(@RequestParam("id") Long id) {
        AdMediaDO adMediaDO = adMediaService.queryById(id);
        if (adMediaDO == null) {
            return CommonResult.success(null);
        }
        return CommonResult.success(BeanUtil.copy(adMediaDO, AdMediaResp.class));
    }

    @Operation(summary = "新增广告媒介")
    @PostMapping("")
    CommonResult<Void> saveAdMedia(@Validated @RequestBody AdMediaReq adMediaReq) {
        return adMediaAppService.saveAdMedia(adMediaReq);
    }

    @Operation(summary = "更新广告媒介")
    @PutMapping("")
    CommonResult<Void> updateAdMedia(@Validated @RequestBody AdMediaReq adMediaReq) {
        return adMediaAppService.updateAdMedia(adMediaReq);
    }

    @Operation(summary = "删除广告媒介")
    @DeleteMapping("")
    CommonResult<Void> delAdMedia(@Validated @RequestBody DeleteIdReq deleteIdReq) {
        return adMediaAppService.delAdMedia(deleteIdReq.getId());
    }

}
